package com.ATM;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/*
要写注释哦!!!
*/
public class ATM {
    private ArrayList<Account> accounts = new ArrayList<>();
    public    Account loginCount;
    Scanner sc = new Scanner(System.in);

    //启动类
    public void stare() {
        System.out.println("=====欢迎来到我的银行=====");
        while (true) {
            System.out.println("请输入以下命令");
            System.out.println("1、登录");
            System.out.println("2、注册");
            System.out.println("3、退出");
            int choose = sc.nextInt();
            switch (choose) {
                case 1 -> {
                    //登录系统
                    System.out.println("请输入你的卡号：");
                    String carId = sc.next();
                    while (true) {
                        System.out.println("请输入你的密码：");
                        String password = sc.next();
                        Account loginCount = login(carId, password);
                        if (loginCount == null) {
                            System.out.println("你输入的密码或卡号有误~~~");
                        } else {
                            Account account = new Account();
                            account.getxing(loginCount);
                            this.loginCount = loginCount;
                            //展示二级菜单
                            secondmenu();
                            break;
                        }
                    }
                }
                case 2 -> {
                    //注册账户
                    register();
                }
                case 3 -> {
                    //退出系统
                    return;
                }
                default -> {
                    System.out.println("你瞎吗？？？重新输入~~~");
                }
            }
        }
    }

    /**
     * 登录账户
     *
     * @param carid    卡号
     * @param password 密码
     * @return 返回成功的账户
     */
    public Account login(String carid, String password) {
        for (int i = 0; i < accounts.size(); i++) {
            Account account = accounts.get(i);
            if (account.getCarId().equals(carid) && account.getPassword().equals(password)) {
                return account;
            }
        }
        return null;
    }

    //二级页面功能
    public void secondmenu() {
        while (true) {
            System.out.println("===请输入您的指令===");
            System.out.println("1、查询账户");
            System.out.println("2、存款");
            System.out.println("3、取款");
            System.out.println("4、转账");
            System.out.println("5、密码修改");
            System.out.println("6、退出");
            System.out.println("7、注销当前账户");
            //获取用户输入的命令
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    //查询账户
                    showAccountAllInfo();
                    break;
                case 2:
                    //存款
                    inMoney();
                    break;
                case 3:
                    //取款
                    toMoney();
                    break;
                case 4:
                    //转账
                    transferMoney();
                    break;
                case 5:
                    //密码修改
                    replacePassword();
                    return;
                case 6:
                    //退出
                    System.out.println("退出当前步骤！");
                    return;
                case 7:
                    //注销当前账户
                    removeCar();
                    break;
                default:
                    System.out.println("您输入的命令有误，请您在(1~7)重新输入！");

            }
        }
    }

    /**
     * 展示账户所有的信息
     *
     * @param
     */
    public void showAccountAllInfo() {
        System.out.println(loginCount.getName() + "您的账号信息如下：");
        System.out.println("卡号：" + loginCount.getCarId());
        System.out.println("密码：" + loginCount.getPassword());
        System.out.println("姓名：" + loginCount.getName());
        System.out.println("性别：" + loginCount.getGender());
        System.out.println("每次取现额度：" + loginCount.getLimit());
        System.out.println("余额：" + loginCount.getMoney());
        System.out.println("地址：" + loginCount.getAddress());
        System.out.println("生日：" + loginCount.getBirthday());
        System.out.println("电话：" + loginCount.getPhon());

    }

    /**
     * 存款功能
     *
     * @param
     */
    public void inMoney() {
        while (true) {
            System.out.println("请输入你要存的金额：");
            double inMoney = sc.nextDouble();
            if (inMoney % 100 != 0) {
                System.out.println("ATM存款只能存整数~~~");
            } else {
                loginCount.setMoney(loginCount.getMoney() + inMoney);
                System.out.println("存款成功！");
                System.out.println("您的总额还有：" + loginCount.getMoney());
                return;
            }
        }

    }

    /**
     * 取钱功能
     *
     * @param
     */
    public void toMoney() {
        while (true) {
            System.out.println("请输入你要取的金额：");
            double toMoney = sc.nextDouble();
            //判断是否符合条件
            if (loginCount.getMoney() < toMoney) {
                System.out.println("余额不足！！！");
            }
            if (loginCount.getLimit() < toMoney) {
                System.out.println("超出限额！！！");
            }
            loginCount.setMoney(loginCount.getMoney() - toMoney);
            System.out.println("取款成功！");
            System.out.println("您的总额还有：" + loginCount.getMoney());
            return;
        }


    }

    /**
     * 转账
     *
     * @param
     */
    public void transferMoney() {
        while (true) {
            System.out.println("请输入你要转账的账号：");
            String distransferCarId = sc.next();
            if (distransferCarId.equals(loginCount.getCarId())) {
                System.out.println("玩呢？？不能给自己转账！！！");
            }
            Account resout = carIdBy(distransferCarId);
            if (resout != null) {
                System.out.println("请输入你要转账的金额：");
                double totransferMoney = sc.nextDouble();
                if (loginCount.getMoney() < totransferMoney) {
                    System.out.println("你的余额不足！！！");
                }
                if (loginCount.getLimit() < totransferMoney) {
                    System.out.println("超出额度了！！！");
                }
                loginCount.setMoney(loginCount.getMoney() - totransferMoney);
                resout.setMoney(resout.getMoney() + totransferMoney);
                System.out.println("您的总额还有：" + loginCount.getMoney());
                return;
            } else {
                System.out.println("你要转账的账号不存在~~~");
            }
        }

    }

    /**
     * 更改密码
     *
     * @param
     */
    public void replacePassword() {
        while (true) {
            System.out.println("请输入原密码");
            String preformPassword = sc.next();
            if (!loginCount.getPassword().equals(preformPassword)) {
                System.out.println("你输入的原密码错误！！！");
            } else {
                System.out.println("请输入更改的密码：");
                String newPassword = sc.next();
                if (loginCount.getPassword().equals(newPassword)) {
                    System.out.println("新密码和旧密码一样，改个锤子啊~~~");
                } else {
                    System.out.println("请输入确认密码：");
                    String reNewPassword = sc.next();
                    if (newPassword.equals(reNewPassword)) {
                        loginCount.setPassword(newPassword);
                        System.out.println("密码修改成功！！！");
                        return;
                    }
                }
            }
        }
    }

    /**
     * 注册账户
     */
    public void register() {
        System.out.println("===注册页面===");
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入性别：");
        String gender = sc.next();
        String password;
        while (true) {
            System.out.println("请设置密码：");
            password = sc.next();
            System.out.println("请确认密码：");
            String repassword = sc.next();
            if (!password.equals(repassword)) {
                System.out.println("你输入的两次密码不一致，请重新输入：");
            } else {
                break;
            }
        }
        System.out.println("请输入限制额度：");
        double limit = sc.nextDouble();
        System.out.println("请输入地址：");
        String address = sc.next();
        System.out.println("请输入生日格式为（yyyy-MM-DD）:");
        String birthday = sc.next();
        System.out.println("请输入电话：");
        String phone = sc.next();
        //生成一个随机卡号
        String carId = carId();

        //数据传入Account对象里面
        Account count = new Account(carId, password, name, gender, limit, 0, address, birthday, phone);
        //添加到集合里面
        accounts.add(count);
        System.out.println(count.getName() + "你的卡号为：" + carId);

    }

    /**
     * 注销账户
     */
    public void removeCar() {
        while (true) {
            System.out.println("请输入要注销的账号的密码：");
            String removeCarPassword = sc.next();
            if (loginCount.getPassword().equals(removeCarPassword)) {
                accounts.remove(removeCarPassword);
                System.out.println("注销成功！！！");
                return;
            }
        }
    }

    /**
     * 随机生成卡号
     *
     * @return
     */
    public String carId() {
        Random r = new Random();
        while (true) {
            String carId = "";
            for (int j = 0; j < 8; j++) {
                int num = r.nextInt(10);
                carId += num;
            }
            Account resout = carIdBy(carId);
            if (resout == null) {
                return carId;
            }
        }

    }

    /**
     * 判断carId是否存在
     *
     * @param carId
     * @return
     */
    public Account carIdBy(String carId) {
        for (int i = 0; i < accounts.size(); i++) {
            Account account = accounts.get(i);
            if (account.getCarId().equals(carId)) {
                return account;
            }
        }
        return null;
    }
}
